import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TypegooseModule } from 'nestjs-typegoose';
import configuration from './config/configuration';
import { LoggingInterceptor } from './interceptors/logging/logging.interceptor';
import { SharedModule } from './modules/shared/shared.module';
import { AdminModule } from './modules/admin/admin.module';
import { PostsController } from './posts/posts.controller';

const IS_DEV: boolean = process.env.NODE_ENV != 'production';
console.log(IS_DEV, '当前环境');
@Module({
  imports: [
    ConfigModule.forRoot({
      envFilePath: IS_DEV
        ? ['.env.development.local', '.env.development', '.env']
        : ['.env.development.local', '.env.production'],
      isGlobal: true,
      ignoreEnvFile: true,
      load: [configuration],
    }),
    // TypeOrmModule.forRoot(),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => ({
        type: 'mysql',
        host: configService.get('DB_HOST', 'localhost'),
        port: configService.get<number>('DB_PORT', 3306),
        username: configService.get('DB_USERNAME', 'root'),
        password: configService.get('DB_PASSWORD', '123456'),
        database: configService.get('DB_DATABASE', 'yst_shop'),
        synchronize: false, // 自动同步数据模型到数据表中 logging: false,
        logging: IS_DEV ? true : false, // 打印日志
        entities: [__dirname + '/**/*.entity{.ts,.js}'],
      }),
    }),
    // TypeOrmModule.forRoot({
    //   type: 'mysql',
    //   host: 'localhost',
    //   port: 3306,
    //   username: 'root',
    //   password: '123456',
    //   database: 'yst_shop',
    //   synchronize: true, // 自动同步数据模型到数据表中 logging: false,
    //   entities: [__dirname + '/**/*.entity{.ts,.js}'],
    // }),
    // TypegooseModule.forRoot('mongodb://8.129.109.91:27017/nest', {
    //   useNewUrlParser: true,
    //   useUnifiedTopology: true,
    //   useCreateIndex: true,
    //   useFindAndModify: true,
    // }),
    AdminModule,
    SharedModule,
  ],
  controllers: [PostsController],
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: LoggingInterceptor,
    },
  ],
})
export class AppModule {}
